import { app, BrowserWindow } from 'electron';
import { join } from 'path';
import { electronApp, is } from '@electron-toolkit/utils';

import { windowManager } from './windowManager/WindowManager';
import { Config } from './configuration/config';

function createWindow(): void {
  const MIN_WIDTH: number = 400;
  const MIN_HEIGHT: number = 100;
  const mainWindow = new BrowserWindow({
    width: MIN_WIDTH,
    height: MIN_HEIGHT,
    show: false,
    titleBarStyle: 'hidden',
    webPreferences: {
      devTools:false,
      contextIsolation: true,
      nodeIntegration: false,
      preload: join(__dirname, '../preload/index.js')
    }
  });
  mainWindow.on('ready-to-show', () => {
    mainWindow.show();
  });
  if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
    mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL']);
  } else {
    mainWindow.loadFile(join(__dirname, '../renderer/index.html'));
  }

  //C:/Users/15613/Desktop/EchoLin/test/specs/configuration/temp-config-too-too.json
  const config = Config.getInstance('./configuration.json');
  new windowManager(mainWindow, config, MIN_HEIGHT).init();
}

app.whenReady().then(() => {
  electronApp.setAppUserModelId('com.electron');
  createWindow();
  app.on('activate', function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});
